package com.taiji.dicom4cloud.smart.camera;

import com.taiji.dicom4cloud.smart.service.SysConfigService;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.Map;

/**
 * @ClassName FtpFileMonitor
 * @Description
 * @Author HX
 * @Date 2021/9/27 14:29
 **/
@Component
public class FtpFileMonitor {

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private FtpFileListener ftpFileListener;

    FileAlterationMonitor monitor = null;

    public FtpFileMonitor() throws Exception {
        monitor = new FileAlterationMonitor(1000);
    }
    //监控方法
    public void monitor(String path, FileAlterationListener listener) {
        FileAlterationObserver observer = new FileAlterationObserver(new File(path));
        monitor.addObserver(observer);
        observer.addListener(listener);
    }
    //监控停止
    public void stop() throws Exception{
        monitor.stop();
    }
    //监控开始
    public void start() throws Exception {
        monitor.start();
    }

    @PostConstruct
    public void run(){
        try {
            Map<String, Object> stringObjectMap = sysConfigService.getConfigByKeys("ftp_dir,mp4_dir,use_dh_cloud");
            FtpFileMonitor ftpFileMonitor = new FtpFileMonitor();
            ftpFileListener.init(stringObjectMap);
            ftpFileMonitor.monitor(stringObjectMap.get("ftp_dir").toString(),ftpFileListener);
            ftpFileMonitor.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
